   const IPaddr = "http://10.141.31.239/Usersmanage";
//const IPaddr = "http://10.139.59.116:8080/Usersmanage";
// const IPaddr = "http://10.139.23.100:8080/Usersmanage";
var token = $.cookie("access_token");

layui.use('upload', function() {
    var $ = layui.jquery
        , upload = layui.upload;

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        , url: '/upload/'
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            //如果上传失败
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            //上传成功
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });
});

function bankCardOnchange() {
    document.getElementById('bankCardNum').value = document.getElementById('bankCardNum').value.replace(/[^\d]/g,'');
    var bankCardNum = document.getElementById('bankCardNum').value;
    var str = "";
    for(var i = 0; i < bankCardNum.length; i++){
        if(i % 4 == 3){
            str += bankCardNum[i];
            str += "&nbsp;";
        }else {
            str += bankCardNum[i];
        }
    }
    document.getElementById('bankCardPlus').innerHTML = str;
}
function bankCardOnblue() {
    document.getElementById('bankCardPlus').style.display = 'none';
    var cardNum = document.getElementById('bankCardNum').value;
    var regex = /^\d+$/;                     //只匹配数字
    if(cardNum == '' || cardNum == null){
        document.getElementById('bankCardError').innerHTML = '请输入银行卡号';
        document.getElementById('bankCardError').style.display = 'block';
    }else if(!regex.test(cardNum)){
        document.getElementById('bankCardError').innerHTML = '银行卡号码包含非数字，请确认后再输入';
        document.getElementById('bankCardError').style.display = 'block';
    }else {
        document.getElementById('bankCardError').style.display = 'none';
    }
}
function bankCardOnfocus() {
    document.getElementById('bankCardPlus').style.display = 'block';
    document.getElementById('bankCardError').style.display = 'none';
    document.getElementById('confirmBankCardError').style.display = 'none';
}
function confirmBankCard() {
    document.getElementById('confirmBankCard').value = document.getElementById('confirmBankCard').value.replace(/[^\d]/g,'');
}
function confirmBankCardOnblur() {
    var cardNum = document.getElementById('bankCardNum').value;
    var confirmCardNum = document.getElementById('confirmBankCard').value;
    var regex = /^\d+$/;       //只匹配数字
    if(confirmCardNum == '' || confirmCardNum == null){
        document.getElementById('confirmBankCardError').innerHTML = '请再一次输入银行卡号';
        document.getElementById('confirmBankCardError').style.display = 'block';
        return false;
    }else if(!regex.test(confirmCardNum)){
        document.getElementById('confirmBankCardError').innerHTML = '银行卡号码包含非数字，请确认后再输入';
        document.getElementById('confirmBankCardError').style.display = 'block';
        return false;
    }else if(cardNum != confirmCardNum){
        document.getElementById('confirmBankCardError').innerHTML = '两次输入的银行卡号不一致，请确认后再输入';
        document.getElementById('confirmBankCardError').style.display = 'block';
        return false;
    }else {
        document.getElementById('confirmBankCardError').style.display = 'none';
        return true;
    }
}
function confirmBankCardOnfocus() {
    document.getElementById('confirmBankCardError').style.display = 'none';
    document.getElementById('bankCardError').style.display = 'none';
}
function bankCardPictOnchange() {
    var fileName = document.getElementById('bankCardPict').value;
    if(fileName == '' || fileName == null){
        document.getElementById('bankCardPictError').innerHTML = '请上传银行卡图片';
        document.getElementById('bankCardPictError').style.display = 'block';
        return false;
    }else {
        document.getElementById('bankCardPictError').style.display = 'none';
        return true;
    }
}

/*银行卡信息上传*/
function bankCardUpload() {
    let bankCardNum = document.getElementById('bankCardNum').value;
    let bankCardPict = document.getElementById('bankCardPict').files[0];
    if(bankCardPictOnchange() && confirmBankCardOnblur()){
        // console.log(12);
        $.ajax({
            url: IPaddr + "/head/updateBank",
            type: "POST",
            async: true,
            headers: {"Authorization": token},
            data: {"file": bankCardPict, "bankNum": bankCardNum, "user": ""},
            success: function(result){
                console.log(result);
                layer.msg('修改银行卡成功');
            },
            error: function (e) {
                // console.log(e);
            },
        })
    }else {
        console.log(234);
    }
}